////////////////////////////////////////////////////////////////////////////////
//
//	This file is part of Woozox, see <http://code.google.com/p/woozox/>.
//
//	Copyright 2008-2009 Florian David
//
//	Woozoo is free software: you can redistribute it and/or modify
//	it under the terms of the GNU Lesser General Public License as 
//	published by the Free Software Foundation, either version 3 of 
//	the License, or (at your option) any later version.
//
//	Woozoo is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU Lesser General Public 
//	License along with Woozoo. If not, see <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////
package org.woozoo.xhtml.wordpress.base
{
	import flash.events.*;
	import flash.net.*;
	
	import org.woozoo.xhtml.core.base.Element;
	import org.woozoo.xhtml.core.base.error.ErrorReason;
	import org.woozoo.xhtml.core.util.StringUtil;
	import org.woozoo.xhtml.wordpress.WPAPI;
	import org.woozoo.xhtml.wordpress.constant.WPConst;
	import org.woozoo.xhtml.wordpress.event.WPCommentEvent;
	import org.woozoo.xhtml.wordpress.theme.WPTheme;
	import org.woozoo.xhtml.wordpress.util.WPDateUtil;

	/**
	 * @private
	 * */ 
	[Event(name="commentSucceed", type="org.woozoo.xhtml.wordpress.event.WPCommentEvent")]

	/**
	 * @private
	 * */ 
	[Event(name="commentFailed", type="org.woozoo.xhtml.wordpress.event.WPCommentEvent")]

	/**
	 * This class represent a comment from a wordpress blog
	 * */
	public class WPComment extends Element
	{
		public var date : Date;
		public var user : WPUser;
		public var content : String;
		
		/**
		 * 
		 * */
		public function WPComment(post : WPPost, source : XML = null) {
			super(post, source);
			if (source) {
				content = (getElementsByTagName(WPTheme.COMMENT_CONTENT_SELECTOR)[0] as Element).innerHTML;
				date = WPDateUtil.dateFromString( StringUtil.stripTags((getElementsByClassName(WPTheme.COMMENT_DATE_SELECTOR)[0] as Element).innerHTML), true);
				user = new WPUser(post, (getElementsByClassName(WPTheme.COMMENT_USER_SELECTOR)[0] as Element).source);
			}
		}
		
		/**
		 * @private
		 * */ 
		public function get isSendable() : Boolean {
			if (user && user.pseudo != "" && user.email != "" && content != "") {
				return true;
			}
			return false;
		}
		
		/**
		 * @private
		 * */ 
		public function send() : Boolean {
			if (!isSendable) {
				trace('Your comment is not valid')
				return false;
			}
			
			var requestVars:URLVariables = new URLVariables();
			requestVars.author = user.pseudo;
			requestVars.email = user.email;
			requestVars.url = user.website.href;
			requestVars.comment = content;
			requestVars.comment_post_ID = (document as WPPost).postId;
			requestVars.comment_parent = 0;
			requestVars.submit = 'Submit Comment';
			
			var url : String = WPAPI.getInstance().getDocumentIndex().url + WPConst.SEND_COMMENT_URL;
			var request : URLRequest = new URLRequest(url);
			request.method = URLRequestMethod.POST;
			request.data = requestVars;
			
			var loader : URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, __loaderCompleteHandler);
			loader.addEventListener(IOErrorEvent.IO_ERROR, __loaderIOErrorHandler);
			loader.load(request);
			return true;
		}
		
		private function __loaderCompleteHandler(e : Event) : void {
			dispatchEvent(new WPCommentEvent(WPCommentEvent.COMMENT_SUCCEED));
		}
		
		private function __loaderIOErrorHandler(e : IOErrorEvent) : void {
			dispatchEvent(new WPCommentEvent(WPCommentEvent.COMMENT_FAILED, new WPCommentErrorReason(ErrorReason.IO_ERROR)));
		}
	}
}